#include "TcpServer.hpp"   // 通信管理，负责建立和断开通信 -> 会话层
#include "Http.hpp"
#include <memory>
#include "Log.hpp"

using namespace log_ns;

HttpResponce Login(HttpRequest& req) {
    // 则执行这个任务
    HttpResponce resp;
    std::cout << "Have got external news" << std::endl;
    req.GetRequestBody();
    std::cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << std::endl;
    resp.AddStatuscode(200, "OK");
    resp.AddContent("<html><h1>result done!</h1></html>");

    // 在这里可以进行程序替换、重定向，执行任务
    // pipe -> dup2 -> fork -> exec*(python/java/php)
    return resp;
}

// 自己建立端口号
int main(int argc, char* argv[]) {
    if (argc != 2) {
        LOG(ERROR, "please input: ./server port\n");
        return 1;
    }
    uint16_t port = std::stoi(argv[1]);
    
    HttpServer http_server;
    http_server.AddService("/login", Login);


    service_t task = std::bind(&HttpServer::HttpServerHandler, &http_server, std::placeholders::_1);

    std::unique_ptr<TcpServer> tsvr = std::make_unique<TcpServer>(task, port);
    tsvr->Init();
    tsvr->Start();

    return 0;
}